
// 129.求根节点到叶节点数字之和
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumNumbers(TreeNode* root) {
        int ret = 0;
        function<void(TreeNode* , int)> dfs = [&](TreeNode* pos , int tmp)
        {
            tmp = tmp*10 + pos->val;
            if(pos->left == nullptr && pos->right == nullptr) ret += tmp;

            if(pos->left) dfs(pos->left , tmp);
            if(pos->right) dfs(pos->right , tmp);
        };
        if(root) dfs(root , 0);
        return ret;
    }
};